<?php
/**
 * KayPaste
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * @category   KayPaste
 * @package    Controller
 * @subpackage Plugins
 * @copyright  Copyright (c) 2007-2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author     Robin Skoglund
 */

/**
 * Imports
 * 
 * @see Zend_Controller_Action_HelperBroker
 * @see Zend_Controller_Plugin_Abstract
 * @see Zend_Translate
 */
require_once 'Zend/Controller/Action/HelperBroker.php';
require_once 'Zend/Controller/Plugin/Abstract.php';

/**
 * Plugin for setting lang in view before dispatch
 *
 * @category   KayPaste
 * @package    Controller
 * @subpackage Plugins
 * @copyright  Copyright (c) 2007-2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author     Robin Skoglund
 */
class Kaypaste_Controller_Plugin_ViewLang extends Zend_Controller_Plugin_Abstract
{
    const DEFAULT_LANG = 'en-US';
    
    /**
     * Authenticate user before dispatch
     *
     * @param  Zend_Controller_Request_Abstract $request
     * @return void
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        if (Zend_Registry::isRegistered('Zend_Translate')) {
            $t = Zend_Registry::get('Zend_Translate');
            if ($t instanceof Zend_Translate ||
                $t instanceof Zend_Translate_Adapter) {
                $lang = $t->getLocale();
                $lang = str_replace('_', '-', $lang);
            } else {
                $lang = self::DEFAULT_LANG;
            }
        } else {
            $lang = self::DEFAULT_LANG;
        }
        
        Zend_Controller_Action_HelperBroker
            ::getStaticHelper('ViewRenderer')->view->lang = $lang;
    }
}
